package com.demo.index;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import com.demo.common.ResultBean;
import com.demo.common.kit.TimeKit;
import com.demo.common.model.Mealallowance;
//import com.demo.common.dto.MenuDto;
import com.jfinal.core.Controller;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.kit.StrKit;

/**
 * IndexController
 */
public class IndexController extends Controller {
	public void index() {
		render("index.html");
	}

	/**
	 * 获取菜单
	 * */
	public void getMenu() {
		/*List<MenuDto> menuList = new ArrayList<MenuDto>();
		MenuDto menu1 = new MenuDto();
		MenuDto menu2 = new MenuDto();
		MenuDto menu3 = new MenuDto();
		MenuDto menu4 = new MenuDto();
		
		menuList.add(menu1);
		menuList.add(menu2);
		menuList.add(menu3);
		menuList.add(menu4);
		
		menu1.setTitle("工时管理");
		menu2.setTitle("日志管理");
		menu3.setTitle("报表管理");
		menu4.setTitle("定时任务");
		
		setAttr("menuList", menuList);*/
		render("menuList.html");
	}
	
	public void list() {
		int page = getParaToInt("page");// 当前页
		int rows = getParaToInt("rows");// 页面的数量
		renderJson(Mealallowance.dao.findPageList(rows, page));
	}

	public void add() {
		ResultBean rb = new ResultBean();
		Mealallowance mealallowance = getModel(Mealallowance.class);
		
		//if(mealallowance.getStartTime())
		
		dealWithMealallowance(mealallowance);
		if (mealallowance.getId() == null) {
			if (mealallowance.save()) {
				rb.setCode(1);
			} else {
				rb.setCode(-1);
				rb.setMsg("保存失败！");
			}
		} else {
			if (mealallowance.update()) {
				rb.setCode(1);
			} else {
				rb.setCode(-1);
				rb.setMsg("修改失败！");
			}
		}

		renderJson(rb);
	}

	public void remove() {
		ResultBean rb = new ResultBean();
		if (Mealallowance.dao.deleteById(getPara()))
			rb.setCode(1);
		else {
			rb.setCode(-1);
			rb.setMsg("删除失败，请刷新后重试!");
		}
		renderJson(rb);
	}

	public void getById() {
		ResultBean rb = new ResultBean();
		String id = getPara();
		if (StrKit.isBlank(id)) {
			rb.setCode(-1);
			rb.setMsg("请选择要修改的对象!");
			renderJson(rb);
			return;
		}
		renderJson(Mealallowance.dao.findById(id));
	}

	public void export() {
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		render(PoiRender.me(Mealallowance.dao.getExpense(startTime, endTime),
				Mealallowance.dao.getTimeDetail(startTime, endTime),
				Mealallowance.dao.getTimeSheetValue(startTime, endTime)).fileName("test.xlsx")
				.sheetName(
						new String[] { "报销表","工时表","工作超时表" })
				.columns(new String[]{"workTimeFormat", "info", "money", "witness", "invoice"}, 
						new String[]{"timeDetail", "jobContent"}, 
						new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"})
				.headers(new String[] { "日期", "摘要", "实际金额", "证明人", "发票" }, 
						new String[]{"时点明细", "工作内容"}, 
						new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"})
				.cellWidth(5000).headerRow(1));
	}

	public void dealWithMealallowance(Mealallowance mealallowance) {
		if (mealallowance.getId() == null) {// 如果id为null，则检查本地数据库，查看当天加班信息是否已经添加
			Mealallowance tmp = Mealallowance.dao.getTodayWork(mealallowance.getWorkTime());
			if (tmp != null)
				tmp.delete();
		}

		// 判断是否是周末
		if(mealallowance.getStartTime() == null) {
			Calendar tmp = Calendar.getInstance();
			tmp.setTime(mealallowance.getWorkTime());
			tmp.set(Calendar.HOUR_OF_DAY, 8);
			tmp.set(Calendar.MINUTE, 30);
			tmp.set(Calendar.SECOND, 0);
			mealallowance.setStartTime(tmp.getTime());
		}
		
		if (TimeKit.isWeekend(mealallowance.getStartTime())) {
			mealallowance.setDuration(
					TimeKit.countOverTimeWeekEnd(mealallowance.getStartTime(), mealallowance.getWorkTime()));
		} else {// 否则设置工作开始时间为当天的八点四十
			// 计算工时
			//判断下班时间是否超过下午7点
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(mealallowance.getWorkTime());
			if(calendar.get(Calendar.HOUR_OF_DAY) < 19) {
				mealallowance.setDuration(new BigDecimal(0));
			} else
				mealallowance.setDuration(TimeKit.countOverTime(mealallowance.getWorkTime()));
		}
		mealallowance.setCreateTime(new Date());
	}

}
